package com.mfc.education.service.oss.service.impl;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.AccessControlList;
import com.aliyun.oss.model.Bucket;
import com.aliyun.oss.model.CannedAccessControlList;
import com.mfc.education.service.oss.service.FileService;
import com.mfc.education.service.oss.util.OssProperties;
import org.apache.commons.lang3.time.DateUtils;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.text.SimpleDateFormat;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.UUID;

/**
 * @author mfc
 * @since 2021/9/3
 */
@Service
public class FileServiceImpl implements FileService {
    @Autowired
    private OssProperties ossProperties;

    @Override
    public String upload(InputStream inputStream, String module, String originalFileName) {

        String endpoint = ossProperties.getEndpoint();
        String bucketName = ossProperties.getBucketname();
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint,
                ossProperties.getKeyid(),
                ossProperties.getKeysecret());
        // 判断bucketname是否存在
        if (!ossClient.doesBucketExist(bucketName)) {
            ossClient.createBucket(bucketName);
            // 设置权限
            ossClient.setBucketAcl(bucketName, CannedAccessControlList.PublicRead);
        }


        // 构建objectName： 文件路径 avatar/2021/9/3/default.jpg
        Date date = new Date();
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat();
        simpleDateFormat.applyPattern("yyyy/MM/dd");
        String folder = simpleDateFormat.format(date);

//        DateTime.now().toString("yyyy/MM/dd");

        String suffix = originalFileName.substring(originalFileName.indexOf("."));
        String fileName = UUID.randomUUID().toString();
        module += "/" + folder;
        String filePath = module + "/" + fileName + suffix;
        // 依次填写Bucket名称（例如examplebucket）和Object完整路径（例如exampledir/exampleobject.txt）。Object完整路径中不能包含Bucket名称。
        ossClient.putObject(bucketName, filePath, inputStream);

        // 关闭OSSClient。
        ossClient.shutdown();


        // 返回url
        // https://remember-me.oss-cn-beijing.aliyuncs.com/avatar/%E8%AF%BE%E8%A1%A8.jpg
        return "https://" + bucketName + "." + endpoint + "/" + filePath;
    }

    @Override
    public void removeFile(String url) {
        String endpoint = ossProperties.getEndpoint();
        String bucketName = ossProperties.getBucketname();
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint,
                ossProperties.getKeyid(),
                ossProperties.getKeysecret());
        String host = "https://" + bucketName + "." + endpoint + "/" ;
//        int i = url.indexOf(endpoint);
//        String objectName = url.substring(endpoint.length() + i);
        String objectName = url.substring(host.length());

        // 删除文件或目录。如果要删除目录，目录必须为空。
        ossClient.deleteObject(bucketName, objectName);

        // 关闭OSSClient。
        ossClient.shutdown();

    }
}
